package splitcity.testApplets;

import java.util.ArrayList;
import java.util.Random;

import controlP5.ControlP5;
import peasy.PeasyCam;
import processing.core.PApplet;
import processing.core.PShape;
import processing.core.PVector;
import splitcity.geom.Face;
import splitcity.geom.FaceQuad;
import splitcity.parameters.Parameter;
import splitcity.parameters.ParameterRandom;
import splitcity.parameters.ParameterSequence;
import splitcity.rules.RuleFootprint;
import splitcity.rules.RuleRoof;
import splitcity.rules.RuleStreetSide;
import splitcity.rules.RuleTorontoBlock;
import splitcity.rules.SplitCityConstants;
import splitcity.util.ProcessingUtil;

public class TestCity extends PApplet implements SplitCityConstants {
	RuleTorontoBlock machine;
	PeasyCam cam;
	ArrayList<Face>faces=new ArrayList<Face>();
	int[]colors=new int[100];
	ControlP5 cp5;
	float sMinParcelWidth=10;
	boolean doExportOBJ;
	boolean doExportDXF;
	
	public void setup() {
		size(800, 600, P3D);
		cam = new PeasyCam(this, 300);
		for (int i = 0; i < colors.length; i++) {
			colors[i] = color(255);
		}
		colors[STREET] = color(100);
		colors[ROOF] = color(255, 0, 0);
		colors[BUILDINGSIDE] = color(0, 0, 255);
		colors[TRACK] = color(100);
		colors[BOARDWALK] = color(100);
		colors[ROOFGABLE] = color(50);
		colors[ROOFFACADE] = color(255,0,0);
		initGeometry();
		machine = new RuleTorontoBlock();
		cp5 = new ControlP5(this);
		cp5.addSlider("sMinParcelWidth").setPosition(20, 20).setRange(3, 40);
		cp5.setAutoDraw(false);
	}
	
	public void display(){
		// we stop camera while mouse over sliders
				if (mouseX < 200) {
					cam.setActive(false);
				} else {
					cam.setActive(true);
				}
				hint(ENABLE_DEPTH_TEST);
				background(0);
				fill(255);
				noStroke();
				pointLight(200,200,200,2000,2000,2000);
				ProcessingUtil.drawWorldAxis3D(g, 200);
				for (int i = 0; i < faces.size(); i++) {
					Face f = faces.get(i);
					if (f.type == -1)
						fill(255);
					else
						fill(colors[f.type]);
					f.display3D(g);
					//f.displayNormal3D(g, 10);
				}
				int s3 = this.millis();

				// change the drawing mode so we can draw the GUI in 2D
				hint(DISABLE_DEPTH_TEST);
				cam.beginHUD();
				noLights();
				cp5.draw();
				cam.endHUD();
	}
	
	public void draw() {
		initGeometry();
		RuleStreetSide ruleStreetSide = (RuleStreetSide) machine.rules
				.get(STREETSIDE);
		ruleStreetSide.minDistance = new Parameter(sMinParcelWidth);
		RuleFootprint ruleFootprint = (RuleFootprint) machine.rules
				.get(FOOTPRINT);
		ruleFootprint.pHeight = new ParameterRandom(10,4,new Random(1));
		//ruleFootprint.pHeight = new ParameterSequence(new float[]{20,20,20,50});
		//RuleRoof ruleRoof = (RuleRoof) machine.rules.get(ROOF);
		//ruleRoof.pHeight=new ParameterRandom(4,3,new Random(1));
		faces = machine.interpret(faces, new Random(1));
		display();
	}
	
	public void initGeometry(){
		faces=new ArrayList<Face>();
		for (int x=-5;x<5;x++){
			for (int y=-5;y<5;y++){
		//FaceQuad quad = new FaceQuad(220*x,70*y,220*(x+1),70*(y+1));
		PVector p0=new PVector(0,0);
		PVector p1=new PVector(220,30);
		PVector p2=new PVector(220,100);
		PVector p3=new PVector(0,70);
		FaceQuad quad = new FaceQuad(p0,p1,p2,p3);
		quad.type=REGION;
		quad.translate(x*220,y*70,0);
		faces.add(quad);
			}
		}
	}

	public void exportOBJ(String filename){
		
	}
	public void keyPressed(){
		
	}
}
